<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>测试微博发布的接口</title>
	</head>
	<body>
		用户名:<input type="text" id="txtUsername" /> 密码:<input type="text" id="txtPassword" />
		<input type="button" id="btnLogin" value="登录" />
		<br  />
		<input type="button" id="btnLogout" value="退出登录" />
		<input type="button" name="btnQuery" id="btnQuery" value="获取自己的微博" />
		<input type="button" name="btnAdd" id="btnAdd" value="发布微博" />
		
		<br  />
		<input type="text" value="" id="txtBlogId">
		<input type="button" name="btnDel" id="btnDel" value="删除微博" />
		<script src="jquery1.11.1.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			var baseUrl = 'http://39.105.222.9:8095';
			$("#btnLogin").click(function(){
				
				$.post(baseUrl + "/blog/login_user/", {"username":$("#txtUsername").val(), "password":$("#txtPassword").val()}, function(data){
					setCookie('token', data.token);
					console.log(data)
				})
			})
			
			// 退出登录
			$("#btnLogout").click(function(){
				var token = getCookie('token');
				$.post(baseUrl + "/blog/logout_user/", {'token': token}, function(data){
					console.log(data);
				})
			})
			
			// 查询自己的微博
			$("#btnQuery").click(function(){
				var token = getCookie('token');
				$.get(baseUrl + "/blog/query_self_blog/", {'token': token}, function(data){
					console.log(data); 
				})
				
			})
			
			// 添加微博
			$("#btnAdd").click(function(){
				var token = getCookie('token');
				var param = {'token': token};
				param['content'] = "诗万首。酒千殇。几曾着眼看侯王？"
				param['portrait'] = 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3626758006,3864948903&fm=26&gp=0.jpg' 
				// 随机生成昵称避免重复
				param['nickname'] = '代号' + new Date().getTime();
				$.post(baseUrl + "/blog/add_blog/", param, function(data){
					// console.log(data);
					if(data.result == 1){
						// 发布后出发点击事件。得到已登录用户发布的微博
						$("#btnQuery").click();
					}else{
						alert("发布失败:" + data.message);
					}
				})
			})
			
			$("#btnDel").click(function(){
				var token = getCookie('token');
				var param = {'token': token, 'id': $("#txtBlogId").val()};
				$.post(baseUrl + "/blog/del_blog/", param, function(data){
					if(data.result == 1){
						
						// 发布后出发点击事件。得到已登录用户发布的微博
						$("#btnQuery").click();
					}else{
						alert("删除失败:" + data.message);
					}
				})
			})
			
			function setCookie(c_name,value,expiredays)
			{
			    var exdate=new Date();
			    exdate.setDate(exdate.getDate()+expiredays);
			    document.cookie = c_name + "=" + escape(value)+
			    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString() + ";path=/;")
			}
			
			function getCookie(c_name)
			{
				if (document.cookie.length > 0){
					c_start = document.cookie.indexOf(c_name + "=")
				    if (c_start != -1){ 
					    c_start = c_start + c_name.length + 1; 
					    c_end = document.cookie.indexOf(";", c_start)
					    if (c_end == -1){
					    	c_end=document.cookie.length
					    }
					    return unescape(document.cookie.substring(c_start, c_end))
				    } 
				}
				return "";
			}
			
			 
		</script>
	</body>
	
</html>
